package com.itheima.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.itheima.pojo.Employee;
import com.itheima.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * @Author: 斌
 * @DateTime: 2023年02月07日  10:05:49
 * @Description: com.itheima.common
 * @Version: 1.0
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    /**
     * 自动填充
     *
     * @param metaObject 元对象
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill ....");
        log.info("当前线程信息:{}", ApplicationContext.threadLocal);
        //this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐使用)
        // 或者


        this.strictInsertFill(metaObject, "createTime", LocalDateTime::now, LocalDateTime.class); // 起始版本 3.3.3(推荐)
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime::now, LocalDateTime.class);
        metaObject.setValue("createUser", ApplicationContext.getData());
        metaObject.setValue("updateUser", ApplicationContext.getData());


    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill ....");
        log.info("当前线程信息:{}", ApplicationContext.threadLocal);

        //设置指定字段的数据
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime::now, LocalDateTime.class);
        //从session获取当前操作用户的id（解决无法直接从session中获取想要数据）
        metaObject.setValue("updateUser", ApplicationContext.getData());
    }

}